# -*- encoding: utf-8 -*-
from modulo.page.models import Section
from django.conf import settings

# Tweets
from ionyse.tweets import search_result, create_list_from_result
import urllib

# Photos
from modulo.gallery.utils import get_images

def menus(request):
    #menus = Section.objects.filter(level__exact=0).filter(menuTop=True).order_by('orderTop')
    menus = Section.objects.select_related().exclude(titleTop__exact='').filter(level__exact=0).filter(menuTop=True).order_by('orderTop')

    sections = []
    for menu in menus:
        menu.subsections = menu.children.exclude(titleLeft__exact='').filter(menuLeft=True).order_by('orderLeft')
        sections.append(menu)

    section = getattr(request, 'section', None)    

    return {'sections': sections,
            'section': section}

def info(request):
    return {'GOOGLE_ID': settings.GOOGLE_ID,
            'SITE_NAME': settings.SITE_NAME,
            'IS_ADMIN': request.user.is_superuser}

def tweets(request):
    """ On récupère les tweets en rapport """
    query=['fimu2010', '#fimu']
    results = search_result(query)
    tweets = create_list_from_result(results)
    return {'tweets': tweets,
            'tweets_url': "http://search.twitter.com/search?q=%s" % "+OR+".join([urllib.quote(q) for q in query])}

def photos(request):
    return {'photos': get_images('bandeau')}
